package com.xqboss.web.controller.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.convert.equipment.EquipmentAttrTemplateConverter;
import com.xqboss.apps.domain.equipment.EquipmentAttrPointRatio;
import com.xqboss.apps.domain.equipment.EquipmentAttrTemplate;
import com.xqboss.apps.domain.nft.Category;
import com.xqboss.apps.dto.IdDto;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.equipment.EquipmentAttrTemplateDto;
import com.xqboss.apps.enums.category.CategoryTypeEnum;
import com.xqboss.apps.enums.nft.TargetTypeEnum;
import com.xqboss.apps.service.equipment.EquipmentAttrPointRatioService;
import com.xqboss.apps.service.equipment.EquipmentAttrTemplateService;
import com.xqboss.apps.service.nft.CategoryService;
import com.xqboss.apps.util.mh.BeanCopierUtil;
import com.xqboss.apps.vo.equipment.EquipmentAttrTemplateDetailVo;
import com.xqboss.apps.vo.equipment.EquipmentAttrTemplateListVo;
import com.xqboss.apps.vo.nft.CategoryVO;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.page.TableDataInfo;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Trent
 * @date 2023/8/16
 */
@Api(tags = "装备-装备基础属性值模板")
@RestController("WebEquipmentTemplateController")
@RequestMapping("/web/equipment/template/attr")
public class EquipmentTemplateController extends BaseController {


    @Autowired
    private EquipmentAttrTemplateService attrTemplateService;
    @Autowired
    private EquipmentAttrPointRatioService attrPointRatioService;
    @Autowired
    private EquipmentAttrTemplateConverter equipmentAttrTemplateConverter;

    @PreAuthorize("@ss.hasPermi('web:equipment:template:attr:list')")
    @ApiOperation("获取初始属性模板列表")
    @GetMapping("list")
    public R<List<EquipmentAttrTemplateListVo>> attrList(PageDto params) {
        params.setOrderByColumn("id");
        params.setSortAsc(false);
        params.startPageAndOrder();
        List<EquipmentAttrTemplate> list = attrTemplateService.list();
        List<EquipmentAttrTemplateListVo> voList = equipmentAttrTemplateConverter.toListVoList(list);
        return R.ok(voList, list);
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:template:attr:detail')")
    @ApiOperation("获取初始属性模板详情")
    @GetMapping("detail/{id}")
    public R<EquipmentAttrTemplateDetailVo> attrDetail(@PathVariable("id")
                                                       @ApiParam(value = "属性模板ID", required = true)
                                                       @NotNull(message = "请输入属性模板ID")
                                                       Long id) {
        EquipmentAttrTemplate attrTemplate = attrTemplateService.getById(id);
        List<EquipmentAttrPointRatio> pointRatioList = attrPointRatioService.list(new LambdaQueryWrapper<EquipmentAttrPointRatio>().eq(EquipmentAttrPointRatio::getEquipmentAttrTemplateId, attrTemplate.getId()));
        EquipmentAttrTemplateDetailVo vo = equipmentAttrTemplateConverter.toDetail(attrTemplate, pointRatioList);
        return R.ok(vo);
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:equipment:template:attr:add')")
    @ApiOperation("添加初始属性模板")
    @PostMapping("add")
    @Log(title = "装备模板", businessType = BusinessType.INSERT)
    public R add(@Validated @RequestBody EquipmentAttrTemplateDto params) {
        attrTemplateService.insert(params);
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:equipment:template:attr:edit')")
    @ApiOperation("修改初始属性模板")
    @PutMapping("edit/{id}")
    @Log(title = "装备模板", businessType = BusinessType.UPDATE)
    public R edit(@PathVariable("id")
                  @ApiParam(value = "属性模板ID", required = true)
                  @NotNull(message = "请输入属性模板ID")
                  Long id,
                  @Validated @RequestBody EquipmentAttrTemplateDto params) {
        attrTemplateService.update(id, params);
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:equipment:template:attr:del')")
    @ApiOperation("删除初始属性模板")
    @DeleteMapping("del/{id}")
    @Log(title = "装备模板", businessType = BusinessType.DELETE)
    public R del(@PathVariable("id")
                 @ApiParam(value = "属性模板ID", required = true)
                 @NotNull(message = "请输入属性模板ID")
                 Long id) {
        attrTemplateService.removeById(id);
        attrPointRatioService.remove(new LambdaQueryWrapper<EquipmentAttrPointRatio>().eq(EquipmentAttrPointRatio::getEquipmentAttrTemplateId, id));
        return R.ok();
    }
}
